package com.xuyiwei.mayi.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by xuyiwei on 2019/4/13.
 */
public class Fanshe {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class<User> class1 = (Class<User>) Class.forName("com.xuyiwei.mayi.test.User");
        Class<User> class2 = User.class;
        Constructor c = class2.getConstructor(String.class,String.class);
        User u  = (User) c.newInstance("111","2222");
        System.out.println(u);

        // 获取到当前的所有属性
        Field[] fields = class2.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName());
        }
        System.out.println("-------------------------------");

        // 获取当前所有的方法
        Method[] declaredMethods = class2.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method.getReturnType());
            System.out.println(method.getName());
        }

        System.out.println("-------------------------------");

        // 获取到当前的所有属性
        Field[] paFs = class2.getFields();
        for (Field field : paFs) {
            System.out.println(field.getName());
        }
        System.out.println("-------------------------------");

        // 获取当前所有的方法
        Method[] paDs = class2.getMethods();
        for (Method method : paDs) {
            System.out.println(method.getName());
        }



        User u1 = class1.newInstance();
        Field idField = class1.getDeclaredField("id");
        idField.setAccessible(true);
        idField.set(u1,"222");
        System.out.println(u1);

    }
}
